package com.mzy.datepackage.M2022.Apr.fourteen;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/4/18 14:22
 * @Version 1.0
 */
public class test {

    public static void main(String[] args) {
        String fileName = " G:\\Java_Source\\navigation_tigra_menu\\demo1\\img\\lev1_arrow.gif ";

        methodOne(fileName);
        System.out.println("====================");
        methodtwo(fileName);
        System.out.println("====================");
        methodThree(fileName);
        System.out.println("====================");
        getFullNameOne();
        System.out.println("====================");
        getFullNameTwo();
        System.out.println("====================");
        getPrefixNameOne();
        System.out.println("====================");
        getPrefixNameTwo();
        System.out.println("====================");
        getPrefixNameThree();
        System.out.println("====================");
        getSuffixNameOne();
        System.out.println("====================");
        getSuffixNameTwo();

    }

    public static void methodOne(String fileName) {

        File tempFile = new File(fileName.trim());
        String fName = tempFile.getName();
        System.out.println("方法一的：fileName = " + fName);

    }

    public static void methodtwo(String fileName) {

        String fName = fileName.trim();
        String NameOne = fName.substring(fName.lastIndexOf("/") + 1);
        System.out.println("方法二的第一种：fileName = " + NameOne);
        String NameTwo = fName.substring(fName.lastIndexOf("\\") + 1);
        System.out.println("方法二的第二种：fileName = " + NameTwo);

    }

    public static void methodThree(String fileName) {

        String fName = fileName.trim();
        ///**split里面必须是正则表达式，"\\"的作用是对字符串转义*/
        String temp[] = fName.split("\\\\");
        String tempName = temp[temp.length - 1];
        System.out.println("方法三的：fileName = " + tempName);

    }

    //获取文件的全名：

    public static void getFullNameOne(){
        String fileName="E:\\file.docx";
        String temp[]=fileName.split("\\\\");
        String fileNameNow=temp[temp.length-1];
        System.out.println(fileNameNow);
    }

    public static void getFullNameTwo(){
        String fileName="E:\\file.pdf";
        String fileNameNow = fileName.substring(fileName.lastIndexOf("\\")+1);
        System.out.println(fileNameNow);
    }

    //获取文件前缀名

    public static void getPrefixNameOne(){

        String filename = "file.docx";
        String caselsh = filename.substring(0,filename.lastIndexOf("."));
        System.out.println(caselsh);
    }

    public static void getPrefixNameTwo(){
        String filename = "file.docx";
        String caselsh = filename.split("\\.")[0];
        System.out.println(caselsh);
    }
    //本方法主要是为了解决split函数不能截取"." 一种曲线救国策略，简单方式是上一种用"\\"转义解决
    public static void getPrefixNameThree(){
        String filename = "file.docx";
        //要是写成：filename.replace(".", "￥");是没有效果的，
        filename = filename.replace(".", "￥");
        String caselsh = filename.split("￥")[0];
        System.out.println(caselsh);
    }
    //获取文件后缀名

    public static void getSuffixNameOne(){
        String filename = "file.txt";
        String[] strArray = filename.split("\\.");
        int suffixIndex = strArray.length -1;
        System.out.println(strArray[suffixIndex]);
    }
    public static void getSuffixNameTwo(){
        String fileName="file.txt";
        String fileTyle=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
        System.out.println(fileTyle);
    }
}